package com.jdew.thkj.view;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ListView;

import com.jdew.thkj.R;

/**
 * 继承google官方刷新控件SwipeRefreshLayout，以实现滑动至底部而触发上拉加载更多的功能。
 * Created by lenovo on 2018/3/5.
 */
public class SwipeRefreshLayoutView extends SwipeRefreshLayout implements AbsListView.OnScrollListener {

    private View mListViewFooter;
    private int mTouchSlop;
    private ListView mListView;
    private boolean isLoading= false;
    //按下时的Y坐标
    private int mYDown;
    //抬起时的Y坐标，与mYDown一起用于滑动到底部时判断是上拉还是下拉
    private int mLastY;
    private OnLoadListener mOnLoadListener;

    public SwipeRefreshLayoutView(Context context) {
        this(context, null);
    }

    public SwipeRefreshLayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获得触发移动事件的最短距离
        mTouchSlop= ViewConfiguration.get(context).getScaledTouchSlop();
        //加载listview底部加载布局
        mListViewFooter= LayoutInflater.from(context).inflate(R.layout.item_footer, null, false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //初始化ListView对象
        if (mListView== null){
            getListView();
        }
    }

    /**
     * 获取ListView对象
     */
    private void getListView() {
        int childs= getChildCount();
        if (childs> 0){
            View childView= getChildAt(0);
            if (childView instanceof ListView){
                mListView= (ListView) childView;
                //设置滚动监听器给ListView，使得滚动的情况下也可以自动加载
                mListView.setOnScrollListener(this);
                Log.d(VIEW_LOG_TAG, "找到ListView");
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    /**
     * 是否可以加载更多，条件是到了最底部，listview不在加载中，且为上拉操作
     * @return
     */
    private boolean canLoad() {
        return isBottom()&& !isLoading&& isPullUp();
    }

    /**
     * 是否是上拉操作
     * @return
     */
    private boolean isPullUp() {
        return (mYDown- mLastY)>= mTouchSlop;
    }

    /**
     * 判断是否到了最底部
     * @return
     */
    private boolean isBottom() {
        if (mListView!= null&& mListView.getAdapter()!= null){
            return mListView.getLastVisiblePosition()== (mListView.getAdapter().getCount()- 1);
        }
        return false;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //滚动时到了最底部也可以加载更多
        if (canLoad()){
            loadData();
        }
    }

    /**
     * 如果到了最底部，而且是上拉操作，那么执行onLoad方法
     */
    private void loadData() {
        if (mOnLoadListener!= null){
            //设置状态
            setLoading(true);
            mOnLoadListener.onLoad();
        }
    }

    public static interface OnLoadListener{
        public void onLoad();
    }

    public void setLoading(boolean loading){
        isLoading= loading;
        if (isLoading){
            mListView.addFooterView(mListViewFooter);
        }else {
            mListView.removeFooterView(mListViewFooter);
            mYDown= 0;
            mLastY= 0;
        }
    }

    public void setOnLoadListener(OnLoadListener loadListener){
        mOnLoadListener= loadListener;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        int action= ev.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                //按下
                mYDown= (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                //移动
                mLastY= (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                //抬起
                if (canLoad()){
                    loadData();
                }
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}

